package com.superquan.selfstudybootmybatis.utils.encrypt;

import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @program: guizhou
 * @ClassName SmsUtils
 * @description: 短信验证码工具类
 * @author: zwq
 **/

@Slf4j
public class SmsUtils {
    /**
     * 生成记录ID
     *
     * @return
     */
    public static String genId() {
        try {
            String currTime = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
            String uuid = UUID.randomUUID().toString();
            return encoderByMd5(currTime + uuid);
        } catch (Exception ex) {
            return null;
        }
    }
    /**
     * 利用MD5进行加密
     */
    private static String encoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());
        return new BigInteger(1, md.digest()).toString(16).substring(8, 24);
    }
}
